<?php
namespace Admin\Controller;
use Think\Controller;
class JurisdictionController extends CommonController {
    public function index(){

    	$users = M('user t1')->join('auth_group_access t2 on t1.id=t2.uid')->join('auth_group t3 on t2.group_id=t3.id')->field('t1.nickname,t1.email,t1.kiss,t1.create_time,t2.*,t3.*')->select();

    	$this->assign('users',$users);
    	
      	$this->display();
    }

    public function roleshow()
    {
    	$groups = M('auth_group')->select();

    	foreach ($groups as $k => $v) {
    		$groups[$k]['rtitle'] = M()->query("select title from auth_rule where id in ({$v['rules']})");

    	}

    	$count = count($groups);

    	$this->assign('count',$count);

    	$this->assign('groups',$groups);

    	$this->display();
    }

    public function cateshow()
    {
    	$cate = M('category_rule')->select();

    	$count = count($cate);

    	$this->assign('cate',$cate);

    	$this->assign('count',$count);

    	$this->display();
    }

    public function ruleshow()
    {
    	$rule = M('category_rule t1')->join('auth_rule t2 on t1.id=t2.cid')->select();

    	$count = count($rule);

    	$category = M('category_rule')->select();

    	$this->assign('category',$category);
    	
    	$this->assign('rule',$rule);

    	$this->assign('count',$count);

    	$this->display();
    }

    public function addshow()
    {
    	$this->display();
    }

    public function adminadd()
    {
    	$data = I('post.');

    	if (M('auth_rule')->add($data)) {
    	 	$arr = array(
                 'error'=>0,
                 'info'=>'添加成功'
             );
             echo json_encode($arr);
             exit;
    	 }else{
    	 	$arr = array(
                 'error'=>1,
                 'info'=>'添加失败'
             );
             echo json_encode($arr);
             exit;
    	 }
    }

    public function addcate()
    {
    	$data = I('post.');

    	if (M('category_rule')->add($data)) {
    	 	$arr = array(
                 'error'=>0,
                 'info'=>'添加成功'
             );
             echo json_encode($arr);
             exit;
    	 }else{
    	 	$arr = array(
                 'error'=>1,
                 'info'=>'添加失败'
             );
             echo json_encode($arr);
             exit;
    	 }
    }

    public function delall()
    {
    	$ids = I('post.ids');

        $ids = rtrim($ids,",");

        if (M('auth_rule')->where("id in(".$ids.")")->delete()){

            $arr = array(
                    'error'=>0,
                    'info'=>'删除成功',
                );
            echo json_encode($arr);
            exit;
        }else{

            $arr = array(
                    'error'=>1,
                    'info'=>'删除失败'

                );
        echo json_encode($arr);
        }
    }

    public function roleaddshow()
    {
    	$cate = M('category_rule t1')->join('auth_rule t2 on t1.id=t2.cid')->select();

    	$str = array();

    	foreach ($cate as $v) {
    		$str[$v['cname']][]=$v;
    	}

    	$this->assign('str',$str);

    	$this->display();
    }

    public function addrole()
    {
    	$data = I('post.');

        $data['rules'] = rtrim(I('post.rules'),',');

        if (M('auth_group')->add($data)) {
            $arr = array(
                 'error'=>0,
                 'info'=>'添加成功'
             );
             echo json_encode($arr);
             exit;
         }else{
            $arr = array(
                 'error'=>1,
                 'info'=>'添加失败'
             );
             echo json_encode($arr);
             exit;
         }
    }
}